//--------------------------------------------------------------------------------------------------------------
// <copyright file="BaseWebPart.cs" company="Microsoft Corporation" author="David Martos" date="01/02/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Reflection;

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

using CSP.Core;
using System.IO;
using Microsoft.Security.Application;

namespace CSP.WebParts
{
    /// <summary>
    /// Abstract class from which all Contoso WebParts inherit. Provides useful methods for WebParts
    /// </summary>
    public abstract class BaseWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        protected CSP.Core.ResourceFile resourceFile;
        public BaseWebPart WrapperWebPart { get; set; }

        protected ArrayList errors = new ArrayList();
        public void AddError(object o)
        {
            errors.Add(o);
        }


        public bool AnyError()
        {
            if (errors.Count > 0)
            {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// Creates a new entry into the standard WSS log file with the given information
        /// </summary>
        /// <param name="errorMessageKey">Resource file key which defines the error message</param>
        /// <param name="errorCategoryKey">Resource file key which defines the error category</param>
        protected void LogMessage(string errorMessageKey, string errorCategoryKey)
        {
            string applicationName = resourceFile.LoadResource(WebPartConstants.CSPErrorApplicationName);
            string errorMessage = resourceFile.LoadResource(errorMessageKey);
            string errorCategory = resourceFile.LoadResource(errorCategoryKey);

            CSP.LoggingEngine.Utils.LogError(errorMessage, errorCategory, applicationName);
        }

        /// <summary>
        /// Creates a new entry into the standard WSS log file with the given information
        /// </summary>
        /// <param name="errorMessageKey">Resource file key which defines the error message</param>
        /// <param name="errorCategoryKey">Resource file key which defines the error category</param>
        protected void LogMessage(string errorMessageKey, string errorCategoryKey, Exception ex)
        {
            string applicationName = resourceFile.LoadResource(WebPartConstants.CSPErrorApplicationName);
            string errorMessage = resourceFile.LoadResource(errorMessageKey);
            string errorCategory = resourceFile.LoadResource(errorCategoryKey);

            CSP.LoggingEngine.Utils.LogError(errorMessage, errorCategory, applicationName, ex);
        }

        /// <summary>
        /// Finds the SharePoint Site associated with the current context
        /// </summary>
        /// <returns>SPWeb object containint the SharePoint Site associated with the current context</returns>
        protected SPWeb GetContextSite()
        {
            return Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
        }

        /// <summary>
        /// Finds the SharePoint Web Collection associated with the current context
        /// </summary>
        /// <returns>SPSite object containing the SharePoint Web Collection associated with the current context</returns>
        protected SPSite GetContextWebCollection()
        {
            return Microsoft.SharePoint.WebControls.SPControl.GetContextSite(Context);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            resourceFile = new ResourceFile();
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Translates a literal into the application language
        /// </summary>
        /// <param name="id">Literal to be translated</param>
        /// <returns>Translation</returns>
        public override string LoadResource(string id)
        {
            string translatedResource;

            try
            {
                translatedResource = resourceFile.LoadResource(id);
            }
            catch
            {
                translatedResource = string.Format(LoadResource("ERROR_Resource_file"), id);
            }

            return translatedResource;
        }

        protected void RenderErrors(System.Web.UI.HtmlTextWriter writer)
        {
            foreach (object o in errors)
            {
                writer.Write(string.Format("<table width=\"100%\"><tr><td><span class=\"csp_webpart_error_message\">" + o.ToString() + "</span></td></tr></table>"));
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (errors.Count == 0)
                base.Render(writer);
            else
                RenderErrors(writer);
        }
    }

}
